package com.dc.boot.autoconfigure.swagger2;

import org.springframework.util.StringUtils;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

/*******************************************************************************
 * 文件名: Swagger2GroupProperties.java.java <br>
 * 版本: <br>
 * 描述: Docket 创建工厂  <br>
 * 版权所有:神州数码<br>
 * //////////////////////////////////////////////////////// <br>
 * 创建者: 王建栋 <br>
 * 创建日期: 2017年8月4日 <br>
 * 修改者: <br>
 * 修改日期: <br>
 * 修改说明: <br>
 ******************************************************************************/
public class DocketFactory {

	/**
	 * 分组
	 */
	private String groupName;
	
	/**
	 * 通用组配置
	 */
	private Swagger2GroupProperties swagger2GroupProperties;
	
	/**
	 * 某一个组的配置
	 */
	private Swagger2Properties swagger2Properties;

	
	public void setGroupName(String groupName){
		this.groupName=groupName;
	} 
	 
	public void setSwagger2Properties(Swagger2Properties swagger2Properties) {
		this.swagger2Properties = swagger2Properties;
	}
	
	public void setSwagger2GroupProperties(
			Swagger2GroupProperties swagger2GroupProperties) {
		this.swagger2GroupProperties = swagger2GroupProperties;
	}

	
	/**
	 * 创建docket 可以和默认配置组合使用
	 * 
	 * 分组必须有package,如果没有则使用默认的package
	 * ant，regex 两个只能使用一种，都配置会使用ant，组和默认配置可以分别使用不同的过滤进行组合。
	 * @return
	 * @throws Exception
	 */
	public Docket getDocket() throws Exception {
	
		 ApiSelectorBuilder builder=new Docket(DocumentationType.SWAGGER_2)
				.groupName(groupName)
		        .apiInfo(buildApiInfo(swagger2GroupProperties, swagger2Properties))
		        .select();
        
		if(StringUtils.hasLength(swagger2Properties.getPackages())){
        	builder.apis(RequestHandlerSelectors.basePackage(swagger2Properties.getPackages()));
        }else if(StringUtils.hasLength(swagger2GroupProperties.getPackages())){
        	builder.apis(RequestHandlerSelectors.basePackage(swagger2GroupProperties.getPackages()));
        }
		
		if(StringUtils.hasLength(swagger2GroupProperties.getAnt())){
			builder.paths(PathSelectors.ant(swagger2GroupProperties.getAnt()));
		}else if(StringUtils.hasLength(swagger2GroupProperties.getRegex())){
			builder.paths(PathSelectors.regex(swagger2GroupProperties.getRegex()));
		}
        
        if(StringUtils.hasLength(swagger2Properties.getAnt())){
        	builder.paths(PathSelectors.ant(swagger2Properties.getAnt()));
        }else if(StringUtils.hasLength(swagger2Properties.getRegex())){
        	builder.paths(PathSelectors.regex(swagger2Properties.getRegex()));
        }else{
        	builder.paths(PathSelectors.any());
        }
		
		return  builder.build();
	}
	
	/**
	 * 获取API信息，可以共用组信息
	 * @param group
	 * @param swagger2Properties
	 * @return
	 */
	 private ApiInfo buildApiInfo(Swagger2GroupProperties group,Swagger2Properties swagger2Properties) {
	       ApiInfoBuilder builder=   new ApiInfoBuilder()
		                .title(group.getTitle())
		                .description(group.getDescription())
		                .contact(group.getContact())
		                .termsOfServiceUrl(group.getTermsOfServiceUrl())
		                .license(group.getLicense())
		                .licenseUrl(group.getLicenseUrl())
		                .version(group.getVersion());
	       
	       if(StringUtils.hasLength(swagger2Properties.getTitle())){
	    	   builder.title(swagger2Properties.getTitle());
	       }
	       if(StringUtils.hasLength(swagger2Properties.getDescription())){
	    	   builder.title(swagger2Properties.getDescription());
	       }
	       if(StringUtils.hasLength(swagger2Properties.getContact())){
	    	   builder.title(swagger2Properties.getContact());
	       }
	       if(StringUtils.hasLength(swagger2Properties.getTermsOfServiceUrl())){
	    	   builder.title(swagger2Properties.getTermsOfServiceUrl());
	       }
	       if(StringUtils.hasLength(swagger2Properties.getLicense())){
	    	   builder.title(swagger2Properties.getLicense());
	       }
	       if(StringUtils.hasLength(swagger2Properties.getVersion())){
	    	   builder.title(swagger2Properties.getVersion());
	       }
		              
	       
	       return  builder.build();
	    }
}
